﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using DataLayer.Model.Classes;
using System.Windows.Media.Imaging;

namespace DataLayer.Model.Objects
{
    [DataContract]
    public class Profile
    {
        private Thread _activeProcess;

        [DataMember]
        public string Name;

        [DataMember]
        public List<Macro> UserMacroses;

        [DataMember]
        public List<WorkProcess> WorkProcesses;

        [DataMember]
        public WorkProcess ActiveProcess;

        [DataMember]
        public Character Character { get; set; }

        [DataMember]
        public BitmapSource Image;

        public Profile() {
        }
        public Profile(Type pClassType, int pLevel)
        {
            if (pClassType.IsAssignableFrom(typeof(Character)))
            {
                throw new ArgumentException("Type " + pClassType +
                                            " is not compatible with Character.");
            }
            else{
                Character = (Character)Activator.CreateInstance(pClassType);
                Character.Level = pLevel;
            }
        }

        public void StartProrecc()
        {
            _activeProcess.Start();

        }

        public void StopProcess()
        {
            _activeProcess.Abort();
        }

        public void ChangeProcess(int pProcessIndex)
        {
            if (_activeProcess.IsAlive)
            {
                _activeProcess.Abort();
                _activeProcess.Name = Name;
                _activeProcess = new Thread(WorkProcesses[pProcessIndex].DoWork);
               
            }
        }

        public void SetActiveProcess(int pProcessIndex){
            if (WorkProcesses.Count > pProcessIndex && WorkProcesses[pProcessIndex] != null)
            {
                
                _activeProcess = new Thread(WorkProcesses[pProcessIndex].DoWork);
                _activeProcess.Name = Name;
            }
        }

        public void AddWorkProcess(WorkProcess pWorkProcess)
        {
            if (WorkProcesses == null)
                WorkProcesses = new List<WorkProcess>();
            WorkProcesses.Add(pWorkProcess);
        }
        public void AddUserMacroses(Macro pMacro)
        {
            if (UserMacroses == null)
                UserMacroses = new List<Macro>();
            UserMacroses.Add(pMacro);
        }
    }
}
